package com.dubbo.order.order_provider_sx.service;

import javax.annotation.Resource;
import javax.transaction.UserTransaction;

import org.springframework.stereotype.Service;
import org.springframework.transaction.jta.JtaTransactionManager;

import com.dubbo.order.order_api_sx.IOrderService;
import com.dubbo.order.order_api_sx.OrderRequest;
import com.dubbo.order.order_api_sx.OrderResponse;
import com.dubbo.order.order_provider_sx.dao.OrderDao;
import com.dubbo.user.user_api_sx.DebitRequest;
import com.dubbo.user.user_api_sx.IUserService;


@Service (value="orderService") 
public class OrderServiceImpl implements IOrderService
{
	@Resource
	private JtaTransactionManager  springTransactionManager;

	@Resource
	private OrderDao orderDao;
	
	@Resource
	private IUserService userService;
	
	public OrderResponse doOrder(OrderRequest orderRuquest) {
		OrderResponse response=new OrderResponse();
		UserTransaction userTransaction = springTransactionManager.getUserTransaction();
		
		try {
			userTransaction.begin();
			orderDao.insertOrder();
			//用户减钱操作
			userService.debit(new DebitRequest());
			userTransaction.commit();
		} catch (Exception e) {
			try {
				userTransaction.rollback();
			} catch (Exception e1) {
				e1.printStackTrace();
			} 
		}
	    response.setCode("000000");
	    return response;
		 
	}

}
